package demoMod.invincibleOne.patches;

import basemod.ReflectionHacks;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.evacipated.cardcrawl.modthespire.lib.SpirePatch;
import com.evacipated.cardcrawl.modthespire.lib.SpireReturn;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.helpers.FontHelper;
import com.megacrit.cardcrawl.screens.SingleCardViewPopup;
import demoMod.invincibleOne.cards.invincible.AbstractInvincibleCard;
import demoMod.invincibleOne.enums.AbstractCardEnum;
import demoMod.invincibleOne.enums.CardTagsEnum;

import java.lang.reflect.Field;

public class SingleCardViewPopupPatch {
    @SpirePatch(
            clz = SingleCardViewPopup.class,
            method = "renderCardTypeText"
    )
    public static class RenderCardTypeText {
        public static SpireReturn<Void> Prefix(SingleCardViewPopup singleCardViewPopup, SpriteBatch sb) {
            AbstractCard card = ReflectionHacks.getPrivate(singleCardViewPopup, SingleCardViewPopup.class, "card");
            if (card != null) {
                String prefix = "";
                if (card.hasTag(CardTagsEnum.VIRUS)) {
                    FontHelper.renderFontCentered(sb, FontHelper.panelNameFont, prefix + AbstractInvincibleCard.cardStrings.EXTENDED_DESCRIPTION[0], (float) Settings.WIDTH / 2.0F + 3.0F * Settings.scale, (float)Settings.HEIGHT / 2.0F - 40.0F * Settings.scale, Color.DARK_GRAY);
                    return SpireReturn.Return(null);
                }
            }
            return SpireReturn.Continue();
        }
    }

    @SpirePatch(
            clz = SingleCardViewPopup.class,
            method = "renderCardBanner"
    )
    public static class PatchRenderCardBanner {
        private static AbstractCard.CardRarity tmpRarity;

        public static void Prefix(SingleCardViewPopup singleCardViewPopup, SpriteBatch sb) {
            AbstractCard card = ReflectionHacks.getPrivate(singleCardViewPopup, SingleCardViewPopup.class, "card");

            if (card != null) {
                tmpRarity = card.rarity;
                if (card.rarity == AbstractCardEnum.LEGENDARY) {
                    card.rarity = AbstractCard.CardRarity.RARE;
                }
            }
        }

        private static Field cardField;

        private static Field yField;

        private static float drawScale = 2.0F;

        private static float yOffsetBase = 690.0F;

        static {
            try {
                cardField = SingleCardViewPopup.class.getDeclaredField("card");
                cardField.setAccessible(true);
                yField = SingleCardViewPopup.class.getDeclaredField("current_y");
                yField.setAccessible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public static void Postfix(SingleCardViewPopup singleCardViewPopup, SpriteBatch sb) {
            AbstractCard card = ReflectionHacks.getPrivate(singleCardViewPopup, SingleCardViewPopup.class, "card");

            if (card != null) {
                card.rarity = tmpRarity;
            }

            if (tmpRarity == AbstractCardEnum.LEGENDARY && card instanceof AbstractInvincibleCard) {
                try {
                    ((AbstractInvincibleCard)card).renderLegendaryText(sb, Settings.WIDTH / 2.0F,
                            (Float) yField.get(singleCardViewPopup), yOffsetBase, drawScale);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
